package mobilesearch.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleAdapter;
import android.widget.ListView;
import android.content.Intent;
import mobilesearch.ui.MobileSearchApplication;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import android.widget.TextView;

public class SearchResultListActivity extends Activity implements OnClickListener {

	private static final String TAG = "SearchResultListActivity";
	private ListView resultList;
	private SimpleAdapter adapter;
	private String listType;
	private List<Map<String, String>> itemList;
	private TextView typeLabel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
		this.setContentView(R.layout.result_activity);
		
		resultList = (ListView)findViewById(R.id.result_list);
		
		try{
			listType = this.getIntent().getAction();
			Log.i(TAG, listType);
		}catch(Exception exception){
			Log.i(TAG, exception.toString());
		}
		getResultList();
		typeLabel = (TextView)findViewById(R.id.type_label);
		typeLabel.setText(listType);
		adapter = new SimpleAdapter(this,getData(),
				R.layout.result_list_item,
				new String[]{"img", "first_item","second_item"},
				new int[]{R.id.img,R.id.first_item,R.id.second_item});
		
		resultList.setAdapter(adapter);

	}
	
	private void getResultList(){
		Log.i(TAG, "getResultList------begin");
		MobileSearchApplication application = (MobileSearchApplication)this.getApplication();
		if(listType.compareTo(this.getString(R.string.application)) == 0){
			itemList = application.getEngine().getAppList();
		}else if(listType.compareTo(this.getString(R.string.audio)) == 0){
			itemList = application.getEngine().getAudioList();
		}else if(listType.compareTo(this.getString(R.string.bookmark)) == 0){
			itemList = application.getEngine().getBookmarkList();
		}else if(listType.compareTo(this.getString(R.string.contacts)) == 0){
			itemList = application.getEngine().getContactsList();
		}else if(listType.compareTo(this.getString(R.string.image)) == 0){
			itemList = application.getEngine().getImageList();
		}else if(listType.compareTo(this.getString(R.string.sms)) == 0){
			itemList = application.getEngine().getSmsList();
		}else if(listType.compareTo(this.getString(R.string.video)) == 0){
			itemList = application.getEngine().getVideoList();
		}else if(listType.compareTo(this.getString(R.string.calllog)) == 0){
			itemList = application.getEngine().getCallLogList();
		}else if(listType.compareTo(this.getString(R.string.email)) == 0){
			itemList = application.getEngine().getEmailList();
		}else if(listType.compareTo(this.getString(R.string.file)) == 0){
			itemList = application.getEngine().getFileList();
		}else if(listType.compareTo(this.getString(R.string.alarm)) == 0){
			itemList = application.getEngine().getAlarmList();
		}
		
		Log.i(TAG, "getResultList------end");
	}
	
	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		int size = itemList.size();
		Log.i(TAG, "getData size"+String.valueOf(size));
		for(int i = 0; i < size; i++){
			Map<String, Object> map = new HashMap<String, Object>();
			try{
				map.put("img", android.R.drawable.btn_star_big_on);
				Map<String, String> item = itemList.get(i);
				map.put("first_item", item.get(MobileSearchEngine.FIRST_ITEM));
				map.put("second_item", item.get(MobileSearchEngine.SECOND_ITEM));
				
		        list.add(map);
			}catch(Exception exception){
				Log.i(TAG, exception.toString());
			}
		}
        
        return list;  
     }  
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.i(TAG, "onStart");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.i(TAG, "onRestart");

	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "onResume");

	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.i(TAG, "onPause");

	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.i(TAG, "onStop");

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i(TAG, "onDestroy");

	}

	@Override
	public void onClick(View v) {

	}

}
